<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class PickupPointApplication extends Model
{
    use HasFactory, SoftDeletes;

    /**
     * 数据表名
     *
     * @var string
     */
    protected $table = 'ims_yz_pickup_point_applications';

    /**
     * 可批量赋值的属性
     *
     * @var array
     */
    protected $fillable = [
        'store_id',
        'status',
        'reason',
        'pickup_point_status',
        'reviewer_id',
        'reviewed_at',
        'submitted_at',
    ];

    /**
     * 属性转换
     *
     * @var array
     */
    protected $casts = [
        'reviewed_at' => 'datetime',
        'submitted_at' => 'datetime',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
        'deleted_at' => 'datetime',
    ];

    /**
     * 状态常量
     */
    const STATUS_PENDING = 'pending';
    const STATUS_APPROVED = 'approved';
    const STATUS_REJECTED = 'rejected';

    /**
     * 自提点状态常量
     */
    const PICKUP_STATUS_ALLOW = 'allow';
    const PICKUP_STATUS_DENY = 'deny';

    

    /**
     * 状态标签映射
     *
     * @var array
     */
    public static $statusLabels = [
        self::STATUS_PENDING => '待审核',
        self::STATUS_APPROVED => '审核通过',
        self::STATUS_REJECTED => '审核不通过',
    ];

    /**
     * 自提点状态标签映射
     *
     * @var array
     */
    public static $pickupStatusLabels = [
        self::PICKUP_STATUS_ALLOW => '正常',
        self::PICKUP_STATUS_DENY => '停用',
    ];

    /**
     * 获取状态标签
     *
     * @return string
     */
    public function getStatusLabelAttribute()
    {
        return self::$statusLabels[$this->status] ?? '未知状态';
    }

    /**
     * 获取自提点状态标签
     *
     * @return string
     */
    public function getPickupStatusLabelAttribute()
    {
        return self::$pickupStatusLabels[$this->pickup_point_status] ?? '未知状态';
    }

    /**
     * 关联门店
     */
    public function store()
    {
        return $this->belongsTo(Store::class, 'store_id');
    }

    /**
     * 审核人关联（如果有管理员表）
     */
    public function reviewer()
    {
        return $this->belongsTo('App\Models\AdminUser', 'reviewer_id');
    }

    /**
     * 作用域：待审核
     */
    public function scopePending($query)
    {
        return $query->where('status', self::STATUS_PENDING);
    }

    /**
     * 作用域：已审核通过
     */
    public function scopeApproved($query)
    {
        return $query->where('status', self::STATUS_APPROVED);
    }

    /**
     * 作用域：审核不通过
     */
    public function scopeRejected($query)
    {
        return $query->where('status', self::STATUS_REJECTED);
    }

    /**
     * 作用域：按提交时间排序
     */
    public function scopeOrderBySubmitted($query, $direction = 'desc')
    {
        return $query->orderBy('submitted_at', $direction);
    }

    /**
     * 作用域：按审核时间排序
     */
    public function scopeOrderByReviewed($query, $direction = 'desc')
    {
        return $query->orderBy('reviewed_at', $direction);
    }
}